<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta name="generator" content=
  "HTML Tidy for Mac OS X (vers 1 September 2005), see www.w3.org" />
  <meta http-equiv="Content-Type" content=
  "text/html; charset=us-ascii" />

  <title>Unit Tests Use Container</title>
</head>

<body>
  <div>
    <h2>Symptoms</h2>

    <p>A test-case class is directly instantiating a PicoContainer
    and registering components within it.</p>
  </div>

  <div>
    <h2>Causes</h2>

    <p>Its too easy to use a PicoContainer instance and setup the
    component to test with it</p>

    <p>It goes really bad when you're setting up consequential
    transitive dependencies For example:</p>

    <div class="source">
      <div>
        <pre>
public void testCocktailWithVodkaIsAlcoholic() {
    DefaultPicoContainer container = new DefaultPicoContainer();
    container.addComponent(Cocktail.class, BananaPopsicleCocktail.class);
    // needed by Cocktail
    container.addComponent(BananaLiqueur.class);
    container.addComponent(OrangeJuice.class);
    container.addComponent(PineappleJuice.class);
    container.addComponent(VanillaVodka.class);
    // needed by Cocktail's ingredients
    container.addComponent(LiqueurMaker.class);
    container.addComponent(StolichnayaDistillery.class);
    container.addComponent(FruitJuiceDistributor.class);

    Cocktail cocktail = container.getComponent(Cocktail.class);

    assertTrue(cocktail.isAlcoholic());
}
</pre>
      </div>
    </div>
  </div>

  <div>
    <h2>What To Do</h2>

    <p>For unit tests like this, the class being tested should be
    instantiated directly by the test. <a href="mock-objects.html"
    title="Mock Objects">Mock Objects</a> should be used to "mock"
    the dependent objects, i.e. supplying a fake implementation
    that can have expectations set and verified on it, rather than
    a real implementation. So, the test becomes:</p>

    <div class="source">
      <div>
        <pre>
public void testCocktailWithVodkaIsAlcoholic() {

    BananaLiqueur bl = mock(BananaLiqueur.class);
    OrangeJuice oj = mock(OrangeJuice.class);
    PineappleJuice pj = mock(PineappleJuice.class);
    VanillaVodka vv = mock(VanillaVodka.class);

    // set expectations these four according to your mocking framework

    Cocktail cocktail = new BananaPopsicleCocktail(bl, oj, pj, vv);
    assertTrue(cocktail.isAlcoholic());

    // verify expectations on the four mocks here.
}
</pre>
      </div>
    </div>

    <p>The implementation details of creating a mock object and
    setting and verifying expectations have been left out of the
    example, as the details depend on which mock object
    library/technique is used. Search for EasyMock, JMock or
    Mockito in Google.</p>
  </div>

  <div>
    <h2>Exceptions</h2>

    <p>The container has to be instantiated somewhere of
    course.</p>

    <h3>Bootstrappers</h3>

    <p>A common place to instantiate containers is in some
    bootstrap class in the application.</p>

    <h1>Functional Tests</h1>

    <p>There may be a requirement to write a high-level
    "functional" test, in which you wish to test the interactions
    between a set of real components (not mocks). In this case, you
    may wish to create a container with the appropriate components
    registered for your test.</p>

  </div>
</body>
</html>
